﻿namespace KnownCMS.Extensions;

static class SiteExtension
{
    internal static async Task AddUserCountAsync(this Database db, BizType type, string userId)
    {
        var user = await db.QueryByIdAsync<CmUser>(userId);
        if (user == null)
            return;

        switch (type)
        {
            case BizType.Content:
                user.ContentQty = (user.ContentQty ?? 0) + 1;
                break;
            case BizType.Reply:
                user.ReplyQty = (user.ReplyQty ?? 0) + 1;
                break;
        }
        await db.SaveAsync(user);
    }

    internal static async Task AddViewLogAsync(this Database db, CmPost post, string ip, string userName)
    {
        var isAdd = await db.AddViewLogAsync(post.Type, post.Id, userName, ip);
        if (!isAdd)
            return;

        post.ViewQty = (post.ViewQty ?? 0) + 1;
        post.CalculateRank();
        await db.SaveAsync(post);
    }
}